//
//  CmtSecMJoinViewController.m
//  Products_B
//
//  Created by lanou on 16/6/4.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "CmtSecMJoinViewController.h"
#import "RequestManager.h"
#define  kUrlString @"http://api.izhangchu.com/"
#import "CmtActModel.h"
#import "CmtSecMJoinTableViewCell.h"
@interface CmtSecMJoinViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)UIButton *footBtn;
@property(nonatomic,assign)NSInteger page;

@property(nonatomic,strong)CmtActModel *dataModel;

@property(nonatomic,strong)NSMutableArray *createArr;

@property(nonatomic,strong)UIView *topView;
@end

@implementation CmtSecMJoinViewController
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    _topView.hidden = YES;
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    _topView.hidden = NO;
}
//返回的方法
-(void)barButtonDismiss:(UIBarButtonItem *)barButton{
    
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 70)];
    _topView.backgroundColor = [UIColor whiteColor];
    [self.navigationController.view addSubview:_topView];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(10, 30, 30, 30);
    [button setTitle:@"<—" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(barButtonDismiss:) forControlEvents:(UIControlEventTouchUpInside)];
    [_topView addSubview:button];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((kWidth - 150)/2, 30, 150, 30)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"他们都参加了";
    [_topView addSubview:label];

    
    self.navigationController.navigationBar.translucent = NO;
//    self.navigationItem.title = @"他们都参加了";
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 64) style:(UITableViewStylePlain)];
//    self.tableView.backgroundColor = [UIColor grayColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    self.createArr = [[NSMutableArray alloc]initWithCapacity:10];
    
    self.page = 1;
    [self requeatDataJoinDataWith:self.page];
    [self setTableViewFootView];
    [self.tableView registerNib:[UINib nibWithNibName:@"CmtSecMJoinTableViewCell" bundle:nil] forCellReuseIdentifier:@"CmtSecMjCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _createArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CmtSecMJoinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CmtSecMjCell" forIndexPath:indexPath];
    CmtActModel *model = self.createArr[indexPath.row];
    cell.model = model;
    return cell;
}


-(void)setFootBtnlabel{
    
    CmtActModel *model = self.createArr.firstObject;
    if ((model.total.integerValue - self.page * 10) < 0) {
        
        self.tableView.tableFooterView = [[UIView alloc]init];
        
        return;
    }
    [self.footBtn setTitle:[NSString stringWithFormat:@"还有%ld人参加",(model.total.integerValue - self.page * 10) ] forState:(UIControlStateNormal)];
}


-(void)setTableViewFootView{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, kHeight - 64 - 50, kWidth, 50)];
    self.footBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.footBtn.frame = CGRectMake((kWidth - 150) / 2, 10, 150, 30);
    self.footBtn.backgroundColor = [UIColor grayColor];
    self.footBtn.layer.cornerRadius = 15;
    
    self.footBtn.tintColor = [UIColor blackColor];
    [footView addSubview:self.footBtn];
    
    self.tableView.tableFooterView = footView;
    [self.footBtn addTarget:self action:@selector(loadMoreData:) forControlEvents:(UIControlEventTouchUpInside)];
    
}


-(void)loadMoreData:(UIButton *)button{
    
    self.page += 1;
    [self setFootBtnlabel];
    [self requeatDataJoinDataWith:self.page];
    
}



-(void)requeatDataJoinDataWith:(NSInteger)page{

    NSDictionary *parDic = @{@"methodName":@"ShequActivityuser",@"version":@"4.3",@"activity_id":_activity_id,@"order_by":@"create_time",@"page":[NSString stringWithFormat:@"%ld",page],@"size":@"10",@"user_id":@"0"};
    [RequestManager requestWithUrl:kUrlString requestType:(RequestTypePOST) parDic:parDic finish:^(NSData *data) {
        NSDictionary *jsdic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSDictionary *dataDic = jsdic[@"data"];
        NSDictionary *dataArr = dataDic[@"data"];
        for (NSDictionary *dic in dataArr) {
            CmtActModel *model = [[CmtActModel alloc]init];
            [model setValuesForKeysWithDictionary:dataDic];
            [model setValuesForKeysWithDictionary:dic];
            [self.createArr addObject:model];
        }
        [self.tableView reloadData];
        [self setFootBtnlabel];
    } error:nil];

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return  90;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
